<template>
    <div v-loading="is_loading"
    element-loading-text="云词生成中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(245,241,247, 0.8)">
      <el-steps :active= "active" style = "padding: 30px;" align-center >
        <el-step title="填写生成文字" icon="el-icon-edit">
          
        </el-step>
        <el-step title="选择背景图" icon="el-icon-picture"></el-step>

        <el-step title="选择ttf" icon="fa fa-font"></el-step>
      </el-steps>

      <div width="200px" class="div1">
          <el-input v-model = "msg" placeholder="输入需要生成的文字内容" style = "width:200px" v-show = "imsg" clearable></el-input>
          <div class="block">
            <!-- <span class="demonstration">有默认值</span>
            <el-color-picker v-model="color1"></el-color-picker> -->
          </div>
          <el-upload class="upload-demo" 
            ref="uploadimg"
            action="/v1/photo/BuildCFont" 
            :auto-upload="false"  
            :http-request="uploadFile"
            clearFiles
            :on-preview="handlePreview" 
            accept=".png" 
            :on-remove="handleRemove" 
            :file-list="fileList" 
            v-show = "input_p"
            v-loading = "is_loading">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              
          <!--     <el-tag v-show="uploadLogo=='1'" >上传中</el-tag>
              <el-tag v-show="uploadLogo=='2'" type="success">上传成功</el-tag>
              <el-tag v-show="uploadLogo=='3'" type="danger">上传失败</el-tag> -->
              <div slot="tip" class="el-upload__tip">请选择透明背景PNG图片</div>
          </el-upload>
          <el-upload class="upload-demo" 
            ref="upload" 
            action="/v1/photo/BuildCFont" 
            :auto-upload="false"  
            :http-request="uploadFile"
            clearFiles
            :on-preview="handlePreview" 
            accept=".ttf" 
            :on-remove="handleRemove" 
            :file-list="fileList" 
            v-show = "input_t"
            v-loading = "is_loading">
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              
              <!-- <el-tag v-show="uploadLogo=='1'" >上传中</el-tag>
              <el-tag v-show="uploadLogo=='2'" type="success">上传成功</el-tag>
              <el-tag v-show="uploadLogo=='3'" type="danger">上传失败</el-tag> -->
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          <el-button class="sub_button" style="margin-left: 10px;" size="small" type="success" @click="submitUpload">{{b_msg}}</el-button>
      </div>
      <div>
        <el-dialog
          title="云词图预览"
          :visible.sync="img_show"
          width="30%"
          :before-close="handleClose">
        <el-card v-show = "img_show">
          <!-- <img class="preview-img " :src="newimg" sizes="100,200"> -->
          <el-image
            style="width: 500px; height: 500px;text-align: center;" :src="newimg" fit="fill" class="image"> </el-image>

        </el-card>
        <a class='cloudimg' :href='newimg' download=""  title="下载云词图" >保存云词图</a>
        </el-dialog>
      </div>
    </div>
</template>

<!-- <el-upload class="upload-demo" 
ref="upload" 
action="/v1/photo/BuildCFont" 
:auto-upload="false"  
:http-request="uploadFile"
clearFiles
:on-preview="handlePreview" 
accept=".ttf,.png,.jpg,.jpeg" 
:on-remove="handleRemove" 
:file-list="fileList" 
v-loading = "is_loading">
  <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">开始制作</el-button>
  <el-tag v-show="uploadLogo=='1'" >上传中</el-tag>
  <el-tag v-show="uploadLogo=='2'" type="success">上传成功</el-tag>
  <el-tag v-show="uploadLogo=='3'" type="danger">上传失败</el-tag>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload> -->



<script>
import axios from 'axios';
import { BuildCFont } from '../../api/autopay'; 
export default {
    name: 'CloudFont',
    data() {
        return {
          fileFormData: null, // 将要上传的formdata数据
          percentage: 0, // 存放上传百分比
          is_loading : false,
          imsg:true,
          input_p:false,
          input_t:false,
          img_show: false,
          color1: '#409EFF',
          active: 1,
          // uploadLogo:1,
          newimg: "static/CloudFont/new_1.png",
          fileData : new FormData(),
          fileList: [],
          b_msg: "下一步",
          msg: "我与父亲不相见已二年余了，我最不能忘记的是他的背影。那年冬天，祖母死了，父亲的差使也交卸了，正是祸不单行的日子，我从北京到徐州，打算跟着父亲奔丧回家。到徐州见着父亲，看见满院狼藉的东西，又想起祖母，不禁簌簌地流下眼泪。父亲说，“事已如此，不必难过，好在天无绝人之路！”回家变卖典质，父亲还了亏空；又借钱办了丧事。这些日子，家中光景很是惨淡，一半为了丧事，一半为了父亲赋闲。丧事完毕，父亲要到南京谋事，我也要回北京念书，我们便同行。到南京时，有朋友约去游逛，勾留了一日；第二日上午便须渡江到浦口，下午上车北去。父亲因为事忙，本已说定不送我，叫旅馆里一个熟识的茶房陪我同去。他再三嘱咐茶房，甚是仔细。但他终于不放心，怕茶房不妥帖；颇踌躇了一会。其实我那年已二十岁，北京已来往过两三次，是没有甚么要紧的了。他踌躇了一会，终于决定还是自己送我去。我两三回劝他不必去；他只说，“不要紧，他们去不好！”我们过了江，进了车站。我买票，他忙着照看行李。行李太多了，得向脚夫行些小费，才可过去。他便又忙着和他们讲价钱。我那时真是聪明过分，总觉他说话不大漂亮，非自己插嘴不可。但他终于讲定了价钱；就送我上车。他给我拣定了靠车门的一张椅子；我将他给我做的紫毛大衣铺好坐位。他嘱我路上小心，夜里警醒些，不要受凉。又嘱托茶房好好照应我。我心里暗笑他的迂；他们只认得钱，托他们直是白托！而且我这样大年纪的人，难道还不能料理自己么？唉，我现在想想，那时真是太聪明了！我说道，“爸爸，你走吧。”他望车外看了看，说，“我买几个橘子去。你就在此地，不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台，须穿过铁道，须跳下去又爬上去。父亲是一个胖子，走过去自然要费事些。我本来要去的，他不肯，只好让他去。我看见他戴着黑布小帽，穿着黑布大马褂，深青布棉袍，蹒跚地走到铁道边，慢慢探身下去，尚不大难。可是他穿过铁道，要爬上那边月台，就不容易了。他用两手攀着上面，两脚再向上缩；他肥胖的身子向左微倾，显出努力的样子。这时我看见他的背影，我的泪很快地流下来了。我赶紧拭干了泪，怕他看见，也怕别人看见。我再向外看时，他已抱了朱红的橘子望回走了。过铁道时，他先将橘子散放在地上，自己慢慢爬下，再抱起橘子走。到这边时，我赶紧去搀他。他和我走到车上，将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土，心里很轻松似的，过一会说，“我走了；到那边来信！”我望着他走出去。他走了几步，回过头看见我，说，“进去吧，里边没人。”等他的背影混入来来往往的人里，再找不着了，我便进来坐下，我的眼泪又来了。近几年来，父亲和我都是东奔西走，家中光景是一日不如一日。他少年出外谋生，独力支持，做了许多大事。那知老境却如此颓唐！他触目伤怀，自然情不能自已。情郁于中，自然要发之于外；家庭琐屑便往往触他之怒。他待我渐渐不同往日。但最近两年的不见，他终于忘却我的不好，只是惦记着我，惦记着我的儿子。我北来后，他写了一信给我，信中说道，“我身体平安，惟膀子疼痛利害，举箸提笔，诸多不便，大约大去之期不远矣。”我读到此处，在晶莹的泪光中，又看见那肥胖的，青布棉袍，黑布马褂的背影。唉！我不知何时再能与他相见！"
        }
    },
    methods: {
      
      submitUpload: function () {
        if (this.active == 1){
          this.active = 2;
          this.imsg = false;
          this.input_p =true;
        }else if (this.active == 2){
          this.active = 3;
          this.input_p =false;
          this.input_t =true;
          this.b_msg = "开始制作";
          this.$refs.uploadimg.submit();
        }else{
          
          var that = this;
          that.is_loading = true;
          that.$refs.upload.submit();
          // that.uploadLogo = "1";
          that.fileData.append("msg",that.msg)
          // axios.post("/v1/photo/BuildCFont",that.fileData,{ retryDelay: 8000 })
          BuildCFont(that.fileData).then(res => {
            let { code, msg, data } = res;
            if (code == 200){

              that.img_show = true;
              that.newimg = data;
              console.log(that.newimg);
              // that.uploadLogo = "2";
            }else{
              that.$message.error(msg);
            }
            that.is_loading = false;
          }).catch(function(error) {
            console.log(error);
            that.$message.error("数据请求失败");
            // that.uploadLogo = "3";
            that.is_loading = false;
          });
          
          that.fileData =  new FormData();
          that.active = 1;
          that.imsg = true;
          that.input_t = false;
          that.b_msg = "下一步";
          that.newimg = "";
        }
      },
      uploadFile(f){
        if (f.file.type === "image/png" || f.file.type ==="image/jpeg"){
          this.fileData.append( "img",f.file);
        }else{
          this.fileData.append( "ttf",f.file);
        }
      
        
      },
      handlePreview(file) {
        // body...
        console.log(file);
      },
      handleClose(done) {
        this.$confirm('确认关闭？').then(_ => {done();}).catch(_ => {});
      },
      handleRemove(file) {
        // body...
      },
      handleSucess(file) {
        // body...
      },
      GetCFont(file){
        console.log(file);
        
        // this.$refs.upload.submit();
        
   
        // let par = {"ttf":this.form.ttf,"img":this.form.img,"msg":this.form.msg}
        // this.is_loading = true
        // BuildCFont(formData).then(res => {
        //   let { code, msg, data } = res;
        // }).catch(() => {
        //   this.$message.error("数据请求失败");
        // });
        // this.is_loading = false
      }

    }

}
</script>

<style type="text/css">
  .div1{
    text-align: center;
    margin-top: 2%;
    margin-bottom: 10%;
  }
  .sub_button{
    margin-top: 10%;
  }
  .cloudimg{
    text-align: center;
    margin-left: 50%;
  }
</style>
